#!/usr/bin/perl -w

print "--- building libsecondlife-cs\n";

system('cd ../../libsecondlife-cs && ./build')
	and die "*** failed to build libsecondlife-cs\n";

print "--- building SLProxy\n";

system('prj2make SLProxy.sln')
	and die "*** prj2make failed\n";

if (-e 'bin/Debug/SLProxy.dll' && -M '../../libsecondlife-cs/bin/Debug/libsecondlife.dll' < -M 'bin/Debug/SLProxy.dll') {
	system('make clean')
		and die "*** make clean failed\n";
}

open(MAKEFILE, 'Makefile')
	or die "*** failed to open Makefile for reading: $!\n";

while (<MAKEFILE>) {
	s/^MCS=mcs$/MCS=gmcs/;
	push @patched, $_;
}

close(MAKEFILE);

open(MAKEFILE, '>Makefile')
	or die "*** failed to open Makefile for writing: $!\n";

print MAKEFILE $_ foreach @patched;

close(MAKEFILE);

system('make', @ARGV)
	and die "*** make failed\n";
